<script type="text/javascript">

	
	function periodOnChange(){
		refreshGrid();
	}
		
	function departmentOnChange(){
		refreshGrid();		
	}
		
	function refreshGrid(){
		$("#tblData").jqGrid("setGridParam",{
				postData:{
					"department_id" :$("#department_id").val(),
					"period" :$("#period").val(),
				},
				datatype:"json"
			}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");		
	}
	
	function newData(){

		
		//var url="zsdsdasd.asdasd";
		var url="index.php?r=keuangan/tu/monthly_fee/formGenerateMF&op=add";
		var winName = "Form Add";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=500,height=400;
		var replace = false,onCenter = true;
		var w = openWindow(winName,"",width,height,onCenter,specs,replace);
		$("#frmData").attr('target',winName);
		$("#frmData").attr('action',url);
		$("#frmData").submit();
		
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				refreshGrid();
			}  
		}, 1000);
	}
	function editRow(id){
		//var url="zsdsdasd.asdasd";
		var url="index.php?r=keuangan/tu/monthly_fee/formGenerateMF&op=edit&id="+ id;
		var winName = "Form Add";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=500,height=400;
		var replace = false,onCenter = true;
		var w = openWindow(winName,"",width,height,onCenter,specs,replace);
		$("#frmData").attr('target',winName);
		$("#frmData").attr('action',url);
		$("#frmData").submit();
		
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				refreshGrid();
			}  
		}, 1000);

	}
	
	function deleteRow(id){		
		if (confirm("Delete data ?")){			
			window.parent.showLoader();			
			//var data = $("#tblTahunAjaran").jqGrid("getRowData",id);
			var editUrl = "index.php?r=keuangan/tu/monthly_fee/editGenerateMF"		
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "del",id:id},
				success: function(data) {
					if(data.actionStatus == 0){
						refreshGrid();
					}else if(data.actionStatus == 1){
						alert(data.actionMessage);
					}else if(data.actionStatus == 2){
						alert(data.actionMessage);
						printConsole(data);
					}
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			
		}
	}
	function generateMF(){
		if (confirm("Process Monthly Fee period " +  $("#period").val() + " ?")){			
			window.parent.showLoader();			
			var url = "index.php?r=keuangan/tu/monthly_fee/generate"		
			$.ajax({	
				url : url,
				type:"GET",
				async:true,
				data: {period:$("#period").val()},
				success: function(data) {
					if(data.actionStatus == 0){
						alert(data.actionMessage);
						refreshGrid();
					}else if(data.actionStatus == 1){
						alert(data.actionMessage);
					}else if(data.actionStatus == 2){
						alert(data.actionMessage);
						printConsole(data);
					}
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			
		}

	}
	function cancelMF(){
		if (confirm("Process Monthly Fee period " +  $("#period").val() + " ?")){			
			window.parent.showLoader();			
			var url = "index.php?r=keuangan/tu/monthly_fee/cancelGenerate"		
			$.ajax({	
				url : url,
				type:"GET",
				async:true,
				data: {period:$("#period").val()},
				success: function(data) {
					if(data.actionStatus == 0){
						alert(data.actionMessage);
						refreshGrid();
					}else if(data.actionStatus == 1){
						alert(data.actionMessage);
					}else if(data.actionStatus == 2){
						alert(data.actionMessage);
						printConsole(data);
					}
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			
		}	
	}
	
	$(function(){		
		$("#tblData").jqGrid({
			url:'index.php?r=keuangan/tu/monthly_fee/generateDetailList',
			postData:{"department_id":'<?php echo $department_id;?>',"period":'<?php echo $period; ?>'},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			shrinkToFit:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			colNames: ['id','SID','Name','Tingkat','Kelas',"Mulai","Sampai",'Total Monthly Fee','Late Charges','Total Disc', 'Invoice','Total Paid','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'sid', index: 'sid', width: 60,search:true},
				{name: 'student_name', index: 'student_name',search:true},
				{name: 'tingkatan_name', index: 'tingkatan_name', width: 100,search:true},
				{name: 'kelas_name', index: 'kelas_name',search:true,width: 100},
				{name: 'start_period', index: 'start_period', width: 40,search:true},
				{name: 'end_period', index: 'end_period', width: 40,search:true},
				{name: 'ttl_monthly_fee', index: 'ttl_monthly_fee', width: 80,template:moneyTemplate},
				{name: 'ttl_late_fee', index: 'ttl_late_fee', width: 80,template:moneyTemplate},
				{name: 'ttl_disc_fee', index: 'ttl_disc_fee', width: 60,template:moneyTemplate},
				{name: 'invoice_no', index: 'invoice_no', width: 100,search:true},
				{name: 'ttl_paid', index: 'ttl_paid', width: 80,search:true,template:moneyTemplate},
				{name: 'act', index: 'act', width: 80, align: 'center'},				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Department',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(" + cl + ")' />"; 
					btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit+btnDelete}) 
				} 
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["sid","student_name","tingkatan_name","kelas_name"]);

		$('#period').monthpicker({
			pattern: 'yyyy-mm',
		});
	});
</script>

<style>
legend {
  padding: 0.2em 0.5em;
  font-size:90%;
  text-align:right;
  border:1px solid #000;
  }
</style>
<div>
	<form id="frmData" method="POST" action = "index.php?r=keuangan/tu/monthly_fee/formGenerateMF&op=add" >
		<div>
			<div style="float:left;width:280px;">			
				<fieldset style="border:1px outset;padding:10px;height:60px;">
					<legend>Generate Monthly Fee</legend>
					<label class="form mandatory" style="width:70px;text-align:right">Periode:</label>
					<input type="text" name="period" id="period" value="<?php  echo $period; ?>" style="width:100px" onchange="periodOnChange()" />
					</br>
					<input type="button" value="Generate" style="margin-left:70px" onclick="generateMF()"/>
					<input type="button" value="Cancel" onclick="cancelMF()"/>
				</fieldset>			
			</div>
			<div style="float:left;width:280px;margin-left:5px">
				<fieldset style="border:1px outset;padding:10px;height:60px">
					<legend>Filter</legend>
					<label class="form mandatory" style="width:70px;text-align:right">Department:</label>
					<select id="department_id" name="department_id"  style="width:100px" onchange="departmentOnChange()">
						<?php
							foreach($arrDepartment as $dept){
								$selected = $dept->id == $department_id ? "selected" :"";
								echo "<option value='$dept->id' $selected>$dept->id</option>";
							} 
						?>
					</select>
				</fieldset>
			</div>
			<div style="clear:both"></div>
		</div>
	</form>
	
	<div style="margin-top:0px;margin-right:20px;">
		<?php 
			$this->widget('application.components.widgets.ActionBar', 
				array("menuAlign" => "right", 
					'items' => array(
						array(
							'id' => "btnRefresh",
							'title' => Yii::t("menu","Segarkan"),
							'icon' => "images/action/Action-Refresh-icon.png",
							'callback'=>"refreshGrid"
						),
						array(
							'id' => "btnAdd",
							'title' => Yii::t("menu","Tambah"),
							'icon' => "images/action/Actions-document-new-icon.png",
							'callback'=>"newData"
						),	
						array(
							'id' => "btnPrint",
							'title' => Yii::t("menu","Cetak"),
							'icon' => "images/action/Actions-document-print-icon2.png",
							'callback'=>"printData"
						),					
						
					)
				)
			);
		?>
		<table id="tblData"></table>
		<div id="navTblData"> </div>
	</div>
	
</div>